#ruby classes
require "oci8"
require "erb"
require "find"
require "logger"
  
#local classes
require "lib/DB_Table.rb"
require "lib/PLSQLGen.rb"

my_plsql_gen = PLSQLGen.new()  
params = my_plsql_gen.load_properties( "conf/PLSQLGen.conf" )

$system_column_names = ["CREATE_DATE", "CREATE_USER", "CREATE_PGM", "UPDATE_DATE", "UPDATE_USER", "UPDATE_PGM"]

#oracle stuff 
conn = OCI8.new( params['db_user'], params['db_password'], params['db_name'])
tableList = Array.new
params['tables'].split( ',' ).each{ |s| tableList.push( conn.describe_table( s.strip ) ) }
conn.logoff

pkg_names = Array.new()
app_abbr = params[ 'app_abbr' ]
app_name = params[ 'app_name' ]

puts "starting template processing"

tableList.each { |tbl|

    $table = tbl
    puts "processing " + tbl.obj_name
    #collect package name
    pkg_names.push( "pkg_" + $table.obj_name.downcase + params["pkg_name_suffix"] )
    #process spec
    begin
      my_plsql_gen.erb_to_file( my_plsql_gen.file_to_string( File.open( params[ 'code_template' ], "r" )) \
        ,params[ 'destination_path' ] +   pkg_names.last + "." + params[ 'pkg_file_type' ] )
      puts "created " + pkg_names.last + "." + params[ 'pkg_file_type' ]
    rescue
      puts "There was a problem processing the template for " + pkg_names.last
      puts $!
    end

}

puts "finished"
